<template>
  <div style="padding: 24px;">
    <div
      style="font-size: 16px;font-weight: 500;color: #1D2129;margin-bottom: 16px;"
    >
      已开展活动/培训项目
    </div>
    <div>
      <div style="display: flex; width: 50%;">
        <div>
          <div style="font-size: 24px;color: #0F0F0F;">{{ number }}</div>
          <div style="font-size: 12px;">共计开展项目/次</div>
        </div>
      </div>
    </div>
    <div id="Chart5" style="width:110%;height:320px;margin-left: -80px;"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";
import { get_sheet6_sport_data } from "@/api/statistics";
export default {
  data() {
    return {
      chartInstance: null,
      coachNumList: [],
      countList: [],
      number: "",
      sportNameList: [],
      teacherNumList: []
    };
  },
  mounted() {
    this.datalist();
  },

  methods: {
    async datalist() {
      await get_sheet6_sport_data().then(res => {
        this.number = res.data.number;
        this.coachNumList = res.data.coachNumList;
        this.countList = res.data.countList;
        this.sportNameList = res.data.sportNameList;
        this.teacherNumList = res.data.teacherNumList;
      });

      // 基于准备好的dom，初始化echarts实例
      var myChart = echarts.init(document.getElementById("Chart5"));
      // 指定图表的配置项和数据
      var option;
      option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            crossStyle: {
              color: "#999"
            }
          }
        },
        toolbox: {},
        legend: {
          data: ["已开展活动/培训类型", "指导教师数量", "教练员数量"]
        },
        xAxis: [
          {
            axisLabel: {
              formatter: val => {
                let txt = val;
                if (val.length > 3) {
                  txt = val.substr(0, 3) + "...";
                }
                return txt;
              }
            },
            type: "category",
            data: [
              "小学",
              "初中",
              "高中",
              "小学、初中一体",
              "初中、高中一体",
              "小初高中一体",
              "中专，职高、技校等"
            ],
            axisPointer: {
              type: "shadow"
            }
          }
        ],
        yAxis: [
          {
            type: "value",
            name: "所",
            min: 0,
            max: 500,
            interval: 100,
            axisLabel: {
              formatter: "{value} "
            }
          },
          {
            type: "value",
            name: "人",
            min: 10,
            max: 100,
            type: "log",
            scale: true,
            interval: 5,
            axisLabel: {
              formatter: "{value} "
            }
          }
        ],
        series: [
          {
            name: "已开展活动/培训类型",
            type: "bar",
            tooltip: {
              valueFormatter: function(value) {
                return value;
              }
            },
            data: this.countList
          },
          {
            name: "指导教师数量",
            type: "line",
            yAxisIndex: 1,
            tooltip: {
              valueFormatter: function(value) {
                return value;
              }
            },
            data: this.teacherNumList
          },
          {
            name: "教练员数量",
            type: "line",
            yAxisIndex: 1,
            tooltip: {
              valueFormatter: function(value) {
                return value;
              }
            },
            data: this.coachNumList
          }
        ]
      };
      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option);
      // 监听父元素大小变化 刷新图表
      var ro = new ResizeObserver(entries => {
        for (let entry of entries) {
          const cr = entry.contentRect;
          myChart.resize();
        }
      });
      ro.observe(Chart5);
    }
  }
};
</script>

<style scoped>
.dianidan {
  width: 7px;
  height: 7px;
  margin-right: 5px;
  margin-top: 3px;
}
</style>
